#!/usr/bin/env python

import sys
import colorama

from _app import App

class Console(App):
    '''Console class

    @author Julot <andy.julot@gmail.com>
    @since Sunday, January 15, 2012. 05:05 PM'''

    _arg = None
    _silent = False
    _verbose = False
    
    def __init__(self, arg):
        super(Console, self).__init__()
        
        self._arg = arg
        
        try: self._verbose = arg.verbose
        except: self._verbose = False
        
        try: self._silent = arg.silent
        except: self._silent = False
        if self._silent: return
        
        colorama.init(autoreset=True)
        
    @property
    def argument(self):
        return self._arg

    def __del__(self):
        if self._silent: return
    
        colorama.deinit()

    def success(self, message):
        if self._silent: return
    
        message = '{0}'.format(message)
        sys.stdout.write(colorama.Fore.GREEN + colorama.Style.BRIGHT + message)
        
    def progress(self, message):
        if self._silent: return
        
        message = '{0}'.format(message)
        sys.stdout.write(colorama.Fore.CYAN + colorama.Style.BRIGHT + message)
        
    def alert(self, message):
        if self._silent: return
        
        s = '{0}'.format(message)
        sys.stdout.write(colorama.Fore.MAGENTA + colorama.Style.BRIGHT + s)
        
    def warning(self, message):
        if self._silent: return
        
        message = '{0}'.format(message)
        sys.stdout.write(colorama.Fore.YELLOW + colorama.Style.BRIGHT + message)
        
    def fail(self, message):
        if self._silent: return
        
        message = '{0}'.format(message)
        sys.stdout.write(colorama.Fore.RED + colorama.Style.BRIGHT + message)
    
    def info(self, message):
        if self._silent: return
        
        message = '{0}'.format(message)
        sys.stdout.write(colorama.Fore.WHITE + colorama.Style.BRIGHT + message)
        
    def ask(self, message):
        s = colorama.Fore.WHITE + colorama.Style.BRIGHT + '%s [yes|no] '
        return raw_input(s % message)[0].lower() == 'y'
    
    def fuss(self, message, mode='info'):
        if self._verbose:
            if message: getattr(self, mode)(message)
    
    def greeting(self, resource):
        self.warning('%s\n' % resource.fullname)
        self.warning('%s\n' % resource.description)
        self.warning('Version %s\n' % resource.version.human)
        date = resource.date.strftime("%A, %B %d, %Y")
        self.warning('Released at %s\n\n' % date)
    
    def farewell(self):
        self.warning('\nGood bye ^_^\n')

